package com.wes.dsptest.audioalarm;

import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class AudioAlarm {
    private final Context context;
    private String CurrentTime;
    private int alarmHour;
    private int alarmMin;
    private boolean bOpenAlarm;
    private String alarmFileName;

    private AlarmManager alarmManager;
    private PendingIntent pendingIntent;
    private final String TAG = "AudioAlarm";

    public AudioAlarm(Context context) {
        this.context = context;
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void openAlarm(boolean set) {
        this.bOpenAlarm = set;
        //打开后按照当前时间闹钟
        if (bOpenAlarm) {
            Intent intent = new Intent(context, AlarmReceiver.class);
            pendingIntent = PendingIntent.getBroadcast(context, 0, intent, FLAG_UPDATE_CURRENT);

            alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            Date now = new Date();
            SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
            String timeStr = format.format(now);
            long left = alarmTimeUtil.getTimeDifference(getStringAlarmTime(), timeStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.MILLISECOND, (int)left);
            long alarmTimeMillis = calendar.getTimeInMillis();
            // 设置闹钟
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTimeMillis, pendingIntent);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTimeMillis, pendingIntent);
            } else {
                alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTimeMillis, pendingIntent);
            }
            // 提示用户
            Toast.makeText(context, "闹钟已设置 : " + alarmTimeMillis, Toast.LENGTH_SHORT).show();
        } else {
            if (alarmManager != null && pendingIntent != null)
                alarmManager.cancel(pendingIntent);
        }
    }

    public boolean getAlarmState() {
        return bOpenAlarm;
    }

    public void setAlarmTime(int hour, int min) {
        this.alarmHour = hour;
        this.alarmMin = min;
    }

    public void setAlarmMusicName(String path) {
        this.alarmFileName = path;
    }

    public int[] getAlarmTime() {
        return new int[]{alarmHour, alarmMin};
    }

    public String getStringAlarmTime() {
        return String.format("%02d:%02d:00", alarmHour, alarmMin);
    }

    public void playMusic() {
        Log.i(TAG, "playMusic:" + alarmFileName);
        MediaPlayer mediaPlayer = new MediaPlayer();
        try {
            File file = new File(alarmFileName);
            mediaPlayer.setDataSource(file.getPath());
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
